﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using Billing.Controls;
using System.Data;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for Amounts.xaml
    /// </summary>
    public partial class Amounts : Window
    {
        List<AmountsControl> mainList = new List<AmountsControl>();
        public Amounts()
        {
            InitializeComponent();

            try
            {
                DataTable dt = Database.GetAmounts();

                if (dt != null && dt.Rows.Count > 0)
                {
                    AmountsControl itemSodexho = new AmountsControl();
                    itemSodexho.lblAmountType.Content = "Sodexho Amount";
                    itemSodexho.lblAmount.Content = dt.Rows[0][0].ToString();
                    mainList.Add(itemSodexho);

                    AmountsControl itemCreditCard = new AmountsControl();
                    itemCreditCard.lblAmountType.Content = "CreditCard Amount";
                    itemCreditCard.lblAmount.Content = dt.Rows[0][1].ToString();
                    mainList.Add(itemCreditCard);
                }
                lbAmountsGrid.ItemsSource = mainList;

                //date.SetFocusOnDate();
                cmbType.Focus();
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.Right && cmbType.IsFocused)
                {
                    amount.Focus();
                }
                else if (e.Key == Key.Left && amount.IsFocused)
                {
                    cmbType.Focus();
                }
                else if (e.Key == Key.F1)
                {
                    if (ValidateFields())
                    {
                        if (MessageBoxResult.OK == MessageBox.Show("Do you want to add this amount for Date: " + new DateTime(date.nudYear.Value, date.nudMonth.Value, date.nudDate.Value).ToString("dd-MMM-yyyy"), "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question))
                        {
                            int result = Database.SaveAmount(new DateTime(date.nudYear.Value, date.nudMonth.Value, date.nudDate.Value), (cmbType.SelectedIndex + 1).ToString(), amount.Text);
                            if (result < 0)
                            {
                                MessageBox.Show("Amount already added.", "Already Added", MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }
                            else if (result == 0)
                            {
                                MessageBox.Show("Some Unknown Error Has Occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }
                            else
                            {
                                MessageBox.Show("Amount Added Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                                amount.Clear();
                                mainList.Clear();
                                lbAmountsGrid.ItemsSource = null;

                                DataTable dt = Database.GetAmounts();

                                if (dt != null && dt.Rows.Count > 0)
                                {
                                    AmountsControl itemSodexho = new AmountsControl();
                                    itemSodexho.lblAmountType.Content = "Sodexho Amount";
                                    itemSodexho.lblAmount.Content = dt.Rows[0][0].ToString();
                                    mainList.Add(itemSodexho);

                                    AmountsControl itemCreditCard = new AmountsControl();
                                    itemCreditCard.lblAmountType.Content = "CreditCard Amount";
                                    itemCreditCard.lblAmount.Content = dt.Rows[0][1].ToString();
                                    mainList.Add(itemCreditCard);
                                }
                                lbAmountsGrid.ItemsSource = mainList;

                                cmbType.SelectedIndex = 0;
                                cmbType.Focus();
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception) { }
        }

        private void date_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter && date.IsFocusedOnHours)
                {
                    cmbType.Focus();
                }
            }
            catch (Exception) { }
        }

        private void cmbType_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    //amount.Focus();
                }
            }
            catch (Exception) { }
        }

        private void amount_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    date.SetFocusOnDate();
                }
                else
                {
                    double value;
                    if (!(double.TryParse(amount.Text, out value)))
                        amount.Clear();
                }
            }
            catch (Exception) { }
        }

        private bool ValidateFields()
        {
            try
            {
                double value;

                if (!(double.TryParse(amount.Text, out value)))
                {
                    MessageBox.Show("Please enter valid amount", "Invalid Amount", MessageBoxButton.OK, MessageBoxImage.Error);
                    amount.Clear();
                    cmbType.Focus();
                    return false;
                }

                DateTime dt = new DateTime(date.nudYear.Value, date.nudMonth.Value, date.nudDate.Value);
                TimeSpan ts = dt - DateTime.Now;

                if ((ts.Days > 0) || (dt.Day != DateTime.Now.Day))
                {
                    MessageBox.Show("Date entered is greater than today's date", "Invalid Date", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                return true;
            }
            catch (Exception) 
            {
                return false;
            }
        }

        private void cmbType_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
